<template>
  <div class="login-wrap">
    <div class="wrap" id="wxLogin">
    </div>
  </div>
</template>

<script>
import { onMounted, ref, defineComponent, nextTick } from 'vue';
import { useRoute } from 'vue-router';
import { usePermissStore } from '../store/permiss';
import { useUserStore } from '../store/user';
import { getCurUserInfo } from '../api/index';
import { useRouter } from 'vue-router';

import { ElMessage, ElMessageBox } from 'element-plus';
export default defineComponent({
  name: 'Login',
  setup(props) {
    const route = useRoute();
    const serviceId = ref('')
    const state = ref('')
    const redirectUri = ref('')

    const setWecomQrCode = () => {
      let url = import.meta.env.VITE_REDIRECT_URL;
      var wwLogin = new WwLogin({
        id: "wxLogin",
        appid: 'wxc2eb61821d5e27b7',
        agentid: '1000127',
        redirect_uri: encodeURIComponent(url),
        state: 1,
        href: "",
        lang: "zh",
      })
    }
    const router = useRouter();

    const permiss = usePermissStore();
    const userStore = useUserStore();
    const goLogin = async values => {
      userStore.handleSetToken(localStorage.getItem('token'))
      let res1 = await getCurUserInfo();
      if (res1.code === 200) {
        let keys;
        if (res1.data?.roleType !== 1) {
          keys = permiss.defaultList['user'];
        } else {
          keys = permiss.defaultList['admin'];
        }
        userStore.handleSetUser(res1.data)
        localStorage.setItem('ms_keys', JSON.stringify(keys));
        localStorage.setItem('ms_user', JSON.stringify(res1.data));
        router.push('/portal');
      }
    }
    const geturl = (url) => {
      let query = url.slice(url.indexOf('?') + 1);
      let objMap = query.split('&');
      let item = objMap.filter(item => item.startsWith('redirectUri'));
      let redirectUri = item.length > 0 ? item[0].split('=')[1] : '';
      return redirectUri;
    }
    onMounted(() => {
      // console.log(encodeURIComponent('http://47.100.103.194:8081/seeyon/ssoController.do?method=loginsso&code=123&code=4b34f5c20bff4605aa7c36cb8f12408a&state=1717419793309'))
      serviceId.value = route.query.serviceId;
      state.value = route.query.state || '';
      redirectUri.value = route.query.redirectUri ? decodeURIComponent(route.query.redirectUri) : '';
      if (serviceId.value === 'sso' || (serviceId.value === undefined && !redirectUri.value && !state.value.toString())) {

      }
      else {

        console.log(redirectUri.value)
        if (!!serviceId.value && !!state.value.toString() && !!redirectUri.value) {
          localStorage.setItem('authorize_obj', JSON.stringify({
            serviceId: serviceId.value,
            state: state.value,
            redirectUri: redirectUri.value,
          }));
        } else {
          ElMessageBox.alert('路径参数serviceId, state, redirectUri字段不能为空!', '提示', {
            confirmButtonText: '确认',
          })
          // ElMessage.warning('路径参数serviceId,state,redirectUri字段不能为空')
        }
      }
      if (localStorage.getItem('token')) {
        goLogin()
      } else {
        setWecomQrCode()
      }


    })
  },
})

</script>

<style scoped>
.checkcode-wrapper {
  display: flex;
}


.wrap {
  padding-top: 20px;
  min-height: 400px;
  left: 50%;
  box-shadow: 6px 6px 16px 16px rgb(12 12 12 / 20%);
  background: #ffffff;
  top: 50%;
  position: absolute;
  transform: translate(-50%, -50%);
}

.login-wrap {
  position: relative;
  width: 100%;
  height: 100%;
  background-color: rgb(45 43 43);
  /* background-image: url('../assets/img/mm-world.jpeg'); */
  background-size: 100%;
}
</style>
